<style lang="less">
    @import './own-space.less';
</style>

<template>
    <div>
        <Form ref="userForm" :model="userForm" :label-width="230" label-position="right" :rules="inforValidate">
            <Card>
                <p slot="title">
                    <Icon type="settings"></Icon>
                    模块配置
                </p>
                <div>
                    <FormItem label="公司名称：" prop="corpname">
                        <div>
                            <Input v-model="userForm.corpname"></Input>
                        </div>
                    </FormItem>
                    <FormItem label="后台项目目录：" prop="servercodelist">
                        <div>
                            <Input v-model="userForm.servercodelist">
                                <Button slot="prepend" icon="android-open" @click="openFolder(userForm.servercodelist)"> 打开</Button>
                            </Input>
                        </div>
                    </FormItem>
                    <FormItem label="VUE前端目录：" prop="clientcodelist">
                        <div>
                            <Input v-model="userForm.clientcodelist">
                                <Button slot="prepend" icon="android-open" @click="openFolder(userForm.clientcodelist)"> 打开</Button>
                            </Input>
                        </div>
                    </FormItem>
                </div>
            </Card>
            <Card style="margin-top:10px;">
                <p slot="title">
                    <Icon type="settings"></Icon>
                    MySQL数据库配置
                </p>
                <div>
                    <FormItem label="MYSQL服务器IP地址：" prop="mysqlhost">
                        <div>
                            <Input v-model="userForm.mysqlhost"></Input>
                        </div>
                    </FormItem>
                    <FormItem label="请输入MYSQL服务器端口：" prop="mysqlport">
                        <div>
                            <Input v-model="userForm.mysqlport"></Input>
                        </div>
                    </FormItem>
                    <FormItem label="请输入MYSQL服务器用户名：" prop="mysqluser">
                        <div>
                            <Input v-model="userForm.mysqluser"></Input>
                        </div>
                    </FormItem>
                    <FormItem label="请输入MYSQL服务器密码：" prop="mysqlpassword">
                        <div>
                            <Input v-model="userForm.mysqlpassword"></Input>
                        </div>
                    </FormItem>
                    <FormItem label="请输入MYSQL服务器数据库名称：" prop="mysqldatabase">
                        <div>
                            <Input v-model="userForm.mysqldatabase"></Input>
                        </div>
                    </FormItem>
                </div>
            </Card>
            </br>
            <div style="width:100%;text-align:center;">
                <Button style="width: 100px;" @click="init">还原信息</Button>
                <Button type="warning" style="width: 100px;" :loading="save_loading" @click="saveEdit">保存修改</Button>
                <Button type="primary" style="width: 100px;" :loading="test_loading" @click="testConn">测试连接</Button>
                <!--<Button style="width: 163px;" @click="archetype('uap30')">初始化项目(UAP3.0)</Button>-->
                <Button type="success" style="width: 163px;margin-left:30px;" @click="archetype('uap316')">初始化项目</Button>
            </div>
        </Form>
    </div>
</template>

<script>
const {dialog} = require('electron').remote
const {ipcRenderer} = require('electron')
export default {
    name: 'ownspace_index',
    data () {
        return {
            save_loading: false,
            test_loading: false,
            userForm: {
                developerconfigurationid: '',
                clientcodelist: '',
                mysqldatabase: '',
                mysqlhost: '',
                mysqlpassword: '',
                mysqlport: '',
                mysqluser: '',
                servercodelist: '',
                userid:'',
                corpname:'',
            },
            inforValidate: {
                servercodelist: [
                    { required: true, message: '请输入项目根目录路径', trigger: 'blur' }
                ],
                mysqlhost: [
                    { required: true, message: '请输入MYSQL服务器IP地址', trigger: 'blur' }
                ],
                mysqlport: [
                    { required: true, message: '请输入MYSQL端口', trigger: 'blur' }
                ],
                mysqluser: [
                    { required: true, message: '请输入MYSQL用户名', trigger: 'blur' }
                ],
                mysqlpassword: [
                    { required: true, message: '请输入MYSQL密码', trigger: 'blur' }
                ],
                mysqldatabase: [
                    { required: true, message: '请输入MYSQL数据库名称', trigger: 'blur' }
                ],
                corpname: [
                    { required: true, message: '请输入公司名称', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        saveEdit () {
            this.$refs['userForm'].validate((valid) => {
                if (valid) {
                    this.saveInfoAjax();
                }
            });
        },
        testConn () {
            //测试MySQL连接参数
            this.test_loading = true;
            ipcRenderer.send('testconn',localStorage.getItem('mysqlhost')+','+localStorage.getItem('mysqluser')+','+localStorage.getItem('mysqlpassword')+','+localStorage.getItem('mysqlport')+','+localStorage.getItem('mysqldatabase'))
        },
        archetype (typename) {

            let self = this;

            if(typename=='uap30'){
                self.$Message.warning({content:'不再支持老版本的UAP3.0骨架初始化！', duration: 4});
                return false;
            }

            this.$Modal.confirm({
                loading:true,
                title:'警告',
                content:'请确认要初始化项目骨架！！！',
                onOk: function(){
                    let modal = this;

                    let param = new FormData(); //创建form对象
                    let projectName = localStorage.getItem('projectname');
                    param.append('projectName',projectName);
                    param.append('corpName',localStorage.getItem('corpName'));
                    param.append('dbName',localStorage.getItem('mysqldatabase'));
                    if("ma-" == projectName.substr(0,3)){
                        self.$axios.post(self.$util.server+'templates/microserviceArcMa', param, {
                            headers: {
                                'authorization': localStorage.getItem('authorization'),
                                "Content-Type":"multipart/form-data"
                            }
                        }).then(function(response) {
                            // 这里是处理正确的回调
                            if (response.data.ok) {
                                self.$Message.success({content:'骨架构建：成功！', duration: 8});
                                let download_url = self.$util.server + response.data.data;
                                console.log('download_url:'+download_url);
                                setTimeout(function(){
                                    ipcRenderer.send('archetype', download_url+',6,1,'+localStorage.getItem('projectname'))
                                },3000)
                            } else {
                                self.$Message.error({content:'代码生成：失败！', duration: 8});
                            }
                        }).catch(function (response) {
                            console.log(response)
                        })
                        modal.remove();
                    }else if("ms-" == projectName.substr(0,3)){
                        self.$axios.post(self.$util.server+'templates/microserviceArcMs', param, {
                            headers: {
                                'authorization': localStorage.getItem('authorization'),
                                "Content-Type":"multipart/form-data"
                            }
                        }).then(function(response) {
                            // 这里是处理正确的回调
                            if (response.data.ok) {
                                self.$Message.success({content:'骨架构建：成功！', duration: 8});
                                let download_url = self.$util.server + response.data.data;
                                console.log('download_url:'+download_url);
                                setTimeout(function(){
                                    ipcRenderer.send('archetype', download_url+',5,1,'+localStorage.getItem('projectname'))
                                },3000)
                            } else {
                                self.$Message.error({content:'代码生成：失败！', duration: 8});
                            }
                        }).catch(function (response) {
                            console.log(response)
                        })
                        modal.remove();
                    }else{
                        self.$Message.error({content:'项目名称异常', duration: 8});
                    }
                }
            });
        },
        init () {
            this.userForm.developerconfigurationid = localStorage.getItem('developerconfigurationid')
            this.userForm.clientcodelist = localStorage.getItem('clientcodelist')!='undefined'?localStorage.getItem('clientcodelist'):'C:'
            this.userForm.mysqldatabase = localStorage.getItem('mysqldatabase')!='undefined'?localStorage.getItem('mysqldatabase'):'ms_base'
            this.userForm.mysqlhost = localStorage.getItem('mysqlhost')!='undefined'?localStorage.getItem('mysqlhost'):localStorage.getItem('serverip')
            this.userForm.mysqlpassword = localStorage.getItem('mysqlpassword')!='undefined'?localStorage.getItem('mysqlpassword'):'root'
            this.userForm.mysqlport = localStorage.getItem('mysqlport')!='undefined'?localStorage.getItem('mysqlport'):'3306'
            this.userForm.mysqluser = localStorage.getItem('mysqluser')!='undefined'?localStorage.getItem('mysqluser'):'root'
            this.userForm.servercodelist = localStorage.getItem('servercodelist')!='undefined'?localStorage.getItem('servercodelist'):'C:'
            this.userForm.corpname = localStorage.getItem('corpName')!='undefined'?localStorage.getItem('corpName'):'sgcc:'
            this.userForm.userid = localStorage.getItem('userid')

            //软件更新
            let latestVersion = null;
            let currentVersion = localStorage.getItem('currentVersion');
            let self = this;
            console.log(11)
            console.log(localStorage.getItem("authorization"))
            console.log(22)

            self.$axios({
              method: "GET",
              url: self.$util.server+'versions/latestversion',
              data: {},
              headers: {
                'authorization': localStorage.getItem('authorization'),
                'Content-Type':'application/json;charset=utf-8'
              }
            }).then(function(response) {
              latestVersion = response.data.version;
              if(currentVersion<latestVersion){
                self.$Modal.confirm({
                  loading:true,
                  title:'软件更新',
                  content:'发现新版本：V'+latestVersion+'！</br></br>更新内容：</br>'+response.data.remark.replace(/\r\n/g,'</br>'),
                  onOk: function(){
                    let modal = this;
                    ipcRenderer.send('versioncontrol',response.data.version+','+self.$util.server+response.data.content)
                    modal.remove();
                  }
                });
              }
            });

            // self.$axios.get(self.$util.server+'versions/latestversion', {}, {
            //     headers: {
            //       // 'authorization': localStorage.getItem('authorization'),
            //       'Content-Type':'application/json;charset=utf-8'
            //     },
            // }).then(function (response) {
            //     latestVersion = response.data.version;
            //     if(currentVersion<latestVersion){
            //         self.$Modal.confirm({
            //             loading:true,
            //             title:'软件更新',
            //             content:'发现新版本：V'+latestVersion+'！</br></br>更新内容：</br>'+response.data.remark.replace(/\r\n/g,'</br>'),
            //             onOk: function(){
            //                 let modal = this;
            //                 ipcRenderer.send('versioncontrol',response.data.version+','+self.$util.server+response.data.content)
            //                 modal.remove();
            //             }
            //         });
            //     }
            // }).catch(function (response) {
            //     console.log(response)
            // })

            //初始化数据
            ipcRenderer.send('init',localStorage.getItem('mysqlhost')+','+localStorage.getItem('mysqluser')+','+localStorage.getItem('mysqlpassword')+','+localStorage.getItem('mysqlport')+','+localStorage.getItem('mysqldatabase')+','+localStorage.getItem('clientcodelist')+','+localStorage.getItem('servercodelist'))

        },
        saveInfoAjax () {
            this.save_loading = true
            let self = this
            let param = new FormData() //创建form对象
            param.append('projectname',localStorage.getItem('projectname'))
            param.append('developerconfigurationid',self.userForm.developerconfigurationid)
            param.append('clientcodelist',self.userForm.clientcodelist)
            param.append('mysqldatabase',self.userForm.mysqldatabase)
            param.append('mysqlhost',self.userForm.mysqlhost)
            param.append('mysqlpassword',self.userForm.mysqlpassword)
            param.append('mysqlport',self.userForm.mysqlport)
            param.append('mysqluser',self.userForm.mysqluser)
            param.append('servercodelist',self.userForm.servercodelist)
            param.append('corpname',self.userForm.corpname)
            param.append('userid',self.userForm.userid)
            self.$axios.post(self.$util.server+'developerconfiguration/save', param, {
                headers: {
                  'authorization': localStorage.getItem('authorization'),
                  "Content-Type":"multipart/form-data"
                }
            }).then(function (response) {
                // 这里是处理正确的回调
                if (response.data.ok) {
                    console.info(response.data.data)

                    //更新本地存储
                    localStorage.setItem('clientcodelist',self.userForm.clientcodelist)
                    localStorage.setItem('mysqldatabase',self.userForm.mysqldatabase)
                    localStorage.setItem('mysqlhost',self.userForm.mysqlhost)
                    localStorage.setItem('mysqlpassword',self.userForm.mysqlpassword)
                    localStorage.setItem('mysqlport',self.userForm.mysqlport)
                    localStorage.setItem('mysqluser',self.userForm.mysqluser)
                    localStorage.setItem('servercodelist',self.userForm.servercodelist)
                    localStorage.setItem('corpName',self.userForm.corpname)

                    //初始化数据
                    ipcRenderer.send('init',localStorage.getItem('mysqlhost')+','+localStorage.getItem('mysqluser')+','+localStorage.getItem('mysqlpassword')+','+localStorage.getItem('mysqlport')+','+localStorage.getItem('mysqldatabase')+','+localStorage.getItem('clientcodelist')+','+localStorage.getItem('servercodelist'))

                    self.$Message.success({content: '保存成功,即时生效！',duration: 2})

                    self.save_loading = false
                } else {
                    return self.$Notice.error({
                        title: '保存失败！',
                        duration: 2
                    })
                }
            }).catch(function (response) {
                console.log(response)
            })
        },
        openFolder(folderpath){
            ipcRenderer.send('openfolder',folderpath)
        }
    },
    mounted () {
        //清除全部消息监听
        ipcRenderer.removeAllListeners('tips')

        //接收消息
        ipcRenderer.on('tips', (event, content) => {
            if(("软件更新成功，更新内容重启软件后生效" == content || content.indexOf("当前版本与指定更新版本相同")!=-1)
                 && latestVersion){
                localStorage.setItem('currentVersion',latestVersion);
            }
            if("MySQL数据库连接：成功！" == content || content == "MySQL数据库连接：失败！"){
                this.test_loading = false;
            }
            this.$Message.info({content: content, duration: 5})
            console.log(content)
        })
        this.init()
    }
};
</script>
